// @algorithm @lc id=1000216 lang=cpp 
// @title SNJvJP


#define print(...)
class Solution {
// @test(8876,217,97)=4
public:
    int orchestraLayout(int num, int xPos, int yPos) {
        // 从外至内 0,1,...,(num+1)/2
        int deep = min( min(xPos, num-1-xPos),
                        min(yPos, num-1-yPos));
        int m = (num-deep*2); // 边长
        int seq = (long(num)*num - long(m)*m)%9;
        print("m=", m, ", deep=", deep, ", seq=", seq, "\n")
        if(xPos == deep){             // 上
            seq += 1 + yPos - deep;
        }else if(yPos == num-1-deep){ // 右
            seq += m + xPos - deep;
        }else if(xPos == num-1-deep){ // 下
            seq += (long(m-1)*2 + num-deep-yPos)%9;
        }else if(yPos == deep){       // 左
            seq += (long(m-1)*3 + num-deep-xPos)%9;
        }
        print("m=", m, ", deep=", deep, ", ans=", seq, "%9=", seq%9, "\n")
        return seq%9 ? seq%9 : 9;
    }
};